package cn.xkai.exercise.a;

/**
 * @description: 爬楼梯
 * 自己的思路：未完成
 * 借鉴的思路：递归会超时，非递归剔除重复计算，斐波那契数列实现
 * @author: kaixiang
 * @date: 2022-07-12
 **/
public class Solution50 {
    public int climbStairs(int n) {
        if (n <= 1)
            return 1;
        if (n < 3)
            return n;
        return climbStairs(n - 1) + climbStairs(n - 2);
    }

    public int climbStairsRefer(int n) {
        if (n <= 2) {
            return n;
        }
        int first = 1, second = 2, sum = 0;
        while (n-- > 2) {
            sum = first + second;
            first = second;
            second = sum;
        }
        return sum;
    }

    public int climbStairsFibonacci(int n) {
        return Fibonacci(n, 1, 1);
    }

    public int Fibonacci(int n, int a, int b) {
        if (n <= 1)
            return b;
        return Fibonacci(n - 1, b, a + b);
    }

    public static void main(String[] args) {
        int n = 5;
        Solution50 solution50 = new Solution50();
        System.out.println(solution50.climbStairs(n));
    }
}
